package com.x.erp.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.City;
import com.x.erp.model.District;
import com.x.erp.model.Province;
import com.x.erp.rpc.CityRpcService;
import com.x.erp.rpc.DistrictRpcService;
import com.x.erp.rpc.ProvinceRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;

/**
 * @author toughPear 区域controller，包括province、city和district
 */
@RestController
@RequestMapping("/area")
public class AreaController extends BaseController {
	@Reference
	private ProvinceRpcService provinceRpcService;
	@Reference
	private CityRpcService cityRpcService;
	@Reference
	private DistrictRpcService districtRpcService;

	@RequestMapping(value = "/province", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel listProvinceAll(Province province) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", provinceRpcService.selectProvinceList(province));
	}

	@RequestMapping(value = "/city", method = { RequestMethod.POST,RequestMethod.GET })
	public ResultModel listCity(City city) {
		if (StringUtils.isEmpty(city.getProvinceId())
				&& StringUtils.isEmpty(city.getProvinceName())
				&& StringUtils.isEmpty(city.getCityPinyin())
				&& StringUtils.isEmpty(city.getCityInitial())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "查询条件不能为空（省id)");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", cityRpcService.selectCityList(city));
	}

	@RequestMapping(value = "/district", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel listDistrict(District district) {
		if (StringUtils.isEmpty(district.getCityId())
				&& StringUtils.isEmpty(district.getProvinceName())
				&& StringUtils.isEmpty(district.getCityName())
				&& StringUtils.isEmpty(district.getDistrictPinyin())
				&& StringUtils.isEmpty(district.getDistrictInitial())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "查询条件不能为空（城市id)");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", districtRpcService.selectDistrictList(district));
	}

	/**
	 * 获取启辕合作省市信息
	 * @param province
	 * @return
	 */
	@RequestMapping(value = "/selectCooperationProvinceList", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel selectCooperationProvinceList(Province province) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", provinceRpcService.selectCooperationProvinceList(province));
	}
}
